<template>
  <div>
    <el-dialog title="导入库区" :visible.sync="dialogVisible" width="30%">
      <el-upload ref="upload" class="upload-demo" drag action="#" :auto-upload="false"
           :http-request="handleFileUpload" :on-success="handleSuccess" :before-upload="beforeUpload" multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <p>仅支持单个表单文件导入</p>
      </el-upload>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { importExcelArea } from '@/api/ManageBaseInfo'
export default {
  data () {
    return {
      dialogVisible: false
    }
  },
  methods: {
    beforeUpload (file) {
      if (!file.name.endsWith('.xlsx')) {
        return alert('请选择excel文件')
      }
      return true
    },
    handleSuccess (data) {
      console.log(data)
    },
    async handleFileUpload (file) {
      const params = new FormData()
      params.append('file', file.file)
      await importExcelArea(params)
    },
    async submit () {
      this.dialogVisible = false
      this.$refs.upload.submit()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-dialog__header {
  box-sizing: border-box;
  height: 56px;
  background: #fff;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  padding: 17px 30px 20px;
  font-weight: 700;
}
/deep/ .el-upload {
  display: flex;
  justify-content: center;
  align-items: center;
}
/deep/ button {
  background: #f8f5f5;
  color: #332929;
  border-radius: 20px;
  border: none;
}
</style>
